$$ = jQuery.noConflict();
String.prototype.replaceAll = function (FindText, RepText) {
    regExp = new RegExp(FindText, "g");
    return this.replace(regExp, RepText);
}

function strtotime(str) {
    var arr = str.split(/[- : \/]/);
    var date = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4], arr[5]);
    // date = new Date(str);苹果不兼容
    return Date.parse(date) / 1000;
}

function drawing(e) {
    clearInterval(app.timer);
    $$("#" + e).find(".djs").html("开奖中");
}
function countdown(e) {
    $$("#" + e).find(".djs").html(format_time(app.ticking, "开奖中"));
    clearInterval(app.timer);
    app.timer = setInterval(function () {
        if (--app.ticking < 0)
            return clearInterval(app.timer),
                void loadplan();
        console.log(format_time(app.ticking, "开奖中"))
        $$("#" + e).find(".djs").html(format_time(app.ticking, "开奖中"));
        //app.element.progress('dsjjdt', (app.progress-app.ticking)/app.progress*100+'%')
    }, 1000)
}

function format_time(t, e) {
    var a = parseFloat(t)
        , n = 0
        , s = 0
        , d = 0
        , r = "<em>分</em>"
        , i = "</span>";
    return null != a && "" != a && (a < 60 ? (d = a,
        a = "<span>00</span>" + r + "<span>" + (d > 9 ? d : "0" + d) + i + "<em>秒</em>") : a >= 60 && a < 3600 ? (s = parseInt(a / 60),
        d = parseInt(a % 60),
        a = "<span>" + (s > 9 ? s : "0" + s) + i + r + "<span>" + (d > 9 ? d : "0" + d) + i + "<em>秒</em>") : a >= 3600 && (n = parseInt(a / 3600),
        s = parseInt(a % 3600 / 60),
        d = parseInt(a % 3600 % 60 % 60),
        a = "<span>" + (n > 9 ? n : "0" + n) + i + "<em>时</em><span>" + (s > 9 ? s : "0" + s) + i + r)),
        0 == s && e ? e : a
}

function calculateMultiple(plan, planList, openhis) {
    if (plan.win === true || plan.win === false)
        return 0;
    var lastWinIssue = plan.beginIssue;
    $$.each(planList, function (i, n) {
        if (n.win === true) {
            lastWinIssue = n.winIssue;
            return false;
        }
        else if (n.win === false) {
            lastWinIssue = n.beginIssue;
        }
    });
    var n = plan.winIssue - lastWinIssue;
    for (n = 0; n < openhis.length && openhis[n].issue != lastWinIssue; n++) ;
    var arr = [1, 3, 9, 27, 81, 243, 243 * 3, 243 * 3 * 3];
    if (n >= arr.length)
        n = arr.length - 1;
    else if (n < 0)
        n = 0;
    return arr[n];
}

function toPlanStr(plans) {
    var planStr = "";
    $$.each(plans, function (i, n) {
        if (i > 0) {
            planStr += " ";
        }
        planStr += n;
    });
    return planStr;
}

function loadplan() {
    if (app.plan == 'hk' || app.plan == 'am'){
        app.type = 'og1lhc';
    }
    if (app.plan == 'bjpk'){
        app.type = 'jspk10';
    }
    if (app.plan == 'jsks'){
        app.type = 'og1k3';
    }
    if (app.plan == 'jnd'){
        app.type = 'ff28';
    }
    if (!app || !app.plan)
        return;
    drawing(app.type);
    $$.getJSON('/plan?url=' + app.plan + "&plan=" + app.did + "&size=20&planSize=20&_t=" + new Date().getTime(), function (ret) {
        if (ret.code == 200) {
            if (ret.opendata[0].expect != $$(".current").html()) {
                app.cplan = ret.plan[0];
                var t = ret.opendata[0].opencode;
                var str = "";
                for (var i in t) {
                    str += '<span class="n' + Number(t[i]) + '">' + Number(t[i]) + "</span>";
                }
                $$(".cai-num").html(str);
                $$(".current").html(ret.opendata[0].expect);
                app.ticking = ret.downtime;
                var cplan = ret.plan[0];
                if (cplan) {

                    $$("#ppchat-plan-info").html('<span style="color:red">' + cplan.start_issue.substr(cplan.start_issue.length - 3) + '-' + cplan.end_issue.substr(cplan.end_issue.length - 3) + '</span>期' + app.planName + ' 【<span style="color:red">' + cplan.forecast_content + '</span>】 ' );
                }
                //$$("#ppchat-plan-info").html(ret.data.cplan);

                var str = "";
                if (ret.plan) {
                    $$.each(ret.plan, function (i, p) {
                        str += p.start_issue.substr(p.start_issue.length - 3) + "-" + p.end_issue.substr(p.end_issue.length - 3) + "期  " + app.planName + " 【" + p.forecast_content + "】 ";
                        if (p.iswin[0] == '中') {
                            str += '<span style="color: #1bd357; ">中</span><br>';
                        }
                        else if (p.iswin[0] == '错') {
                            str += '<span style="color: #e82f2f; ">错</span><br>';
                        }
                        else {
                            str += '<span style="color: #ffa200; ">等开</span><br>';
                        }
                    });
                    $$("#plans-txt").html('<ul id="jihua_list">' + str + '</ul>');
                }
                else if (ret.data.article) {
                    $$("#plans-txt").html(ret.data.article);
                }
                else {
                    $$("#plans-txt").html("");
                }
                str = "";
                $$.each(ret.opendata, function (i, r) {
                    var codes = "";
                    $$.each(r.opencode, function (i, r) {
                        codes += '<span class="n' + Number(r) + '">' + Number(r) + "</span>";
                    });
                    str += '<tr class="tr" ><td class="time"><span>' + r.expect + '</span></td><td class="td-num td-' + app.plan + '"><div class="cai-num  ' + app.type + '-num">' + codes + '</div></td></tr>';
                });
                $$("#ppchat-opens tbody").html(str);
                countdown(ret.type);
            } else {
                setTimeout('loadplan()', 3000);
            }

        } else {
            setTimeout('loadplan()', 3000);
        }
    });
}

$$(document).ready(function () {
    app.planName = $$('.mui-title').html()
    $$('.mui-title').html($$("#ppchat-plan-select li").html());
    app.planName = $$("#ppchat-plan-select li").html()
    $$("#ppchat-plan-select li").click(function () {
        $$(".current").html('');
        mui('.mui-backdrop').popover('hide');
        $$(".mui-backdrop").hide();
        app.cplan = "";
        app.did = $$(this).data('did');
        $$('.mui-title').html($$(this).html());
        app.planName = $$(this).html()
        loadplan();
    });
    loadplan();
});
